﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

#if !SILVERLIGHT
using UIT.WPF.Converters;

namespace UIT.WPF.Renderers
#else
using UIT.Silverlight.Converters;

namespace UIT.Silverlight.Renderers
#endif
{
    public class GridRenderer :
#if !SILVERLIGHT
 WpfRenderer<Controls.Containers.Grid, Grid>
#else
 SlRenderer<Controls.Containers.Grid, Grid>
#endif
    {
        public override void Render(Controls.Containers.Grid control)
        {
            Result = new Grid();
            int r = 0, c;
            foreach (UIT.Controls.Containers.Row row in control.Rows)
            {
                RowDefinition def;
                Result.RowDefinitions.Add(def = new RowDefinition());
#if!SILVERLIGHT
                def.SetBinding(RowDefinition.HeightProperty, new Binding("Height") { Converter = new UnitConverter(), Source=row });
                //def.SetBinding(RowDefinition.MaxHeightProperty, new Binding("MaxHeight") { Converter = new UnitConverter(), Source=row });
                //def.SetBinding(RowDefinition.MinHeightProperty, new Binding("MinHeight") { Converter = new UnitConverter(), Source=row });
#else
                UnitConverter converter = new UnitConverter();
                if (!row.Height.IsEmpty)
                    def.Height = UnitConverter.Convert(row.Height);
                if (!row.MaxHeight.IsEmpty)
                    def.MaxHeight = UnitConverter.Convert(row.MaxHeight).Value;
                if (!row.MinHeight.IsEmpty)
                    def.MinHeight = UnitConverter.Convert(row.MinHeight).Value;
#endif
                c = 0;
                foreach (Controls.Containers.Cell cell in row.Cells)
                {
                    if (Result.ColumnDefinitions.Count <= c)
                        Result.ColumnDefinitions.Add(new ColumnDefinition());
                    System.Windows.UIElement cellUI = Manager.Render(cell.Child);
#if !SILVERLIGHT
                    Grid.SetColumn(cellUI, c);
                    Grid.SetRow(cellUI, r);
#else
                    Grid.SetColumn((System.Windows.FrameworkElement)cellUI, c);
                    Grid.SetRow((System.Windows.FrameworkElement)cellUI, r);
#endif
                    Result.Children.Add(cellUI);
                    c++;
                }
                r++;
            }
        }
    }
}
